//
//  CityModel.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/15.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "CityModel.h"
#import "AFNetworking.h"
#import "InterFace.h"
#import "EncryptionData.h"
#import "ProductModel.h"
#import "CategoryModel.h"
#import "JSONKit.h"


@implementation CityModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
#pragma mark 获取省网络请求
- (void)getProvinceConnetcationRequest:(NSString *)interface cityArr:(NSMutableArray *)array{
    
    __weak CityModel *cityModel = self;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];
    [manager POST:interface parameters:NULL success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSString *str = dic[@"sign"];
        
        @try {
            
            EncryptionData *data = [[EncryptionData alloc]init];
            NSString *datastr = [data decryption:str :PasswordKey];
            
            NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
            
            NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
            
            //返回成功
            if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
                 [array removeAllObjects];
                NSLog(@"向服务器请求省市区 ＝＝＝ %@",dataDic);
                
                                NSArray *myDatArr =dataDic[@"data"];
                
                                for (int i = 0; i <[myDatArr count]; i++) {
                
                                    NSDictionary *productDic = myDatArr[i];
                                    CityModel *model = [[CityModel alloc] init];
                
                                    [model setValuesForKeysWithDictionary:productDic];
                
                                    [array addObject:model];
                                }
                [cityModel.cityModelDelegate getCityInterfaceEnd];
            } else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        }
        @catch (NSException *exception) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"========== %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}
#pragma -mark市／县网络请求
- (void)getCityConnetcationRequestcity:(NSString *)interface cityArr:(NSDictionary *)parameter modelArray:(NSMutableArray *)array{
    
    __weak CityModel *cityModel = self;
    
    

    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
  
    NSLog(@"请求市区参数 ＝＝ %@",parameter);
    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    NSDictionary *paramer = @{@"sign":str};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager POST:interface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str = responseObject[@"sign"];
        @try {
            
            EncryptionData *data = [[EncryptionData alloc]init];
            NSString *datastr = [data decryption:str :PasswordKey];
            
            NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
            
            NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
            
            //返回成功
            if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
                [array removeAllObjects];
                
                NSLog(@"向服务器请求省市区 ＝＝＝ %@",dataDic);
                
                NSArray *myDatArr =dataDic[@"data"];
                
                for (int i = 0; i <[myDatArr count]; i++) {
                    
                    NSDictionary *productDic = myDatArr[i];
                    CityModel *model = [[CityModel alloc] init];
                    
                    [model setValuesForKeysWithDictionary:productDic];
                    
                    [array addObject:model];
                }
                [cityModel.cityModelDelegate getCityInterfaceEnd];
            } else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:dataDic[@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        }
        @catch (NSException *exception) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"========== %@", [error description]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
    }];
    
}
#pragma mark - 加密
-(NSString *)encodeString:(NSString *)data key:(NSString *)key{
    //    NSData* sampleData = [data dataUsingEncoding:NSUTF8StringEncoding];
    //
    //    NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
    NSLog(@"%@",data);
    NSString *result=[[NSMutableString alloc] init];
    NSInteger keyLength = [key length];
    int position = 0;
    int keyChar = 0;
    int strChar = 0;
    for (int i = 0; i < [data length]; i++) {
        position = i % keyLength;
        strChar = [data characterAtIndex:i];
        keyChar =[key characterAtIndex:position];
        int  ch = strChar ^ keyChar;
        char ca = (char) ch;
        result=[result stringByAppendingString:[NSString stringWithFormat:@"%C",ca]];
    }
    
    NSData* sampleDat1a = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString * base64String1 = [sampleDat1a base64EncodedStringWithOptions:0];
    
    return base64String1;
}
@end
